﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Models.Tables;

namespace WebUI.Areas.Admin.Controllers
{
    public class SystemController : Controller
    {
        MainContext db = new MainContext();
        protected override void OnActionExecuting(ActionExecutingContext context)
        {
            ViewBag.Href = "/Admin/System?current=0";
            base.OnActionExecuting(context);
        }

        [Authorize]
        public ActionResult Index()
        {
            return View(db.SystemParams.FirstOrDefault());
        }

        [Authorize, HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SaveMain(SystemParam m)
        {
            try
            {
                var record = db.SystemParams.FirstOrDefault();
                record.LastModifiedDataTime = DateTime.Now;
                record.LastModifiedUserID = int.Parse(User.Identity.Name);
                record.SiteName = m.SiteName;
                record.SiteTitle = m.SiteTitle;
                record.SiteKeywords = m.SiteKeywords;
                record.SiteDescription = m.SiteDescription;
                record.SiteLogo = m.SiteLogo;
                record.ICP = m.ICP;
                record.Tel = m.Tel;
                record.Fax = m.Fax;
                record.QQ = m.QQ;
                record.Email = m.Email;
                if (db.SaveChanges() == 1)
                    return View("Result", true);
                return View("Result", false);
            }
            catch (Exception)
            {
                return View("Result", false);
            }
        }

        [Authorize, HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SaveDisplay(SystemParam m)
        {
            try
            {
                var record = db.SystemParams.FirstOrDefault();
                record.LastModifiedDataTime = DateTime.Now;
                record.LastModifiedUserID = int.Parse(User.Identity.Name);
                record.DisplayProduct = m.DisplayProduct;
                record.DisplayArticle = m.DisplayArticle;
                record.DisplayGuestbook = m.DisplayGuestbook;
                record.HomeDisplayProduct = m.HomeDisplayProduct;
                record.HomeDisplayArticle = m.HomeDisplayArticle;
                record.ThumbWidth = m.ThumbWidth;
                record.ThumbHeight = m.ThumbHeight;
                record.PriceDecimal = m.PriceDecimal;
                if (db.SaveChanges() == 1)
                    return View("Result", true);
                return View("Result", false);
            }
            catch (Exception)
            {
                return View("Result", false);
            }
        }

        [Authorize, HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SaveDefined(SystemParam m)
        {
            try
            {
                var record = db.SystemParams.FirstOrDefault();
                record.LastModifiedDataTime = DateTime.Now;
                record.LastModifiedUserID = int.Parse(User.Identity.Name);
                record.DefinedProduct = m.DefinedProduct;
                record.DefinedArticle = m.DefinedArticle;
                if (db.SaveChanges() == 1)
                    return View("Result", true);
                return View("Result", false);
            }
            catch (Exception)
            {
                return View("Result", false);
            }
        }
    }
}
